//
//  Constants.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import Foundation
import UIKit
import SnapKit
import UITextView_Placeholder
let Screen_Width =  UIScreen.main.bounds.width
let Screen_Height =  UIScreen.main.bounds.height
let keyWindow = UIApplication.shared.keyWindow
let THEM_COLOR = #colorLiteral(red: 0.9529411765, green: 0.6470588235, blue: 0.6470588235, alpha: 1)
let TITLE_COLOR = #colorLiteral(red: 0.8862745098, green: 0.1098039216, blue: 0.1098039216, alpha: 1)
let NAV_BG_COLOR = #colorLiteral(red: 0.9529411765, green: 0.6470588235, blue: 0.6470588235, alpha: 1)
let NAV_TINT_COLOR = #colorLiteral(red: 0.1019607843, green: 0.1019607843, blue: 0.1019607843, alpha: 1)
let NAV_TITLE_COLOR = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
let TAB_BG_COLOR =  #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
let TAB_SELECT_TITLE_COLOR =  #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
let TAB_NORMAL_TITLE_COLOR =  #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1)
let VIEW_BG_COLOR = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
let Vehicle_Type = ["A1","A2","B1","B2","C1","C2","D1","D2"]
let WEATHERS = ["Clear up","thunderstorm","cloudy","Overcast sky","spit"]
let WEATHER_ICONS = ["weather_clear_up","weather_thunderstorm","weather_cloudy","weather_overcast_sky","weather_spit"]
let WEATHER_SMALL_ICONS = ["clear_up_icon","thunderstorm_icon","cloudy_icon","overcast_sky_icon","spit_icon"]
typealias RequestSuccess = () ->Void
typealias RequestFailed = (_ msg: String, _ code:Int) ->Void
extension UIDevice {
    
    /// 顶部安全区高度
    static func Top_safeDistance() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else { return 0 }
            return window.safeAreaInsets.top
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.top
        }
        return 0;
    }
    
    /// 底部安全区高度
    static func Bottom_safeDistance() -> CGFloat {
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let window = windowScene.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        } else if #available(iOS 11.0, *) {
            guard let window = UIApplication.shared.windows.first else { return 0 }
            return window.safeAreaInsets.bottom
        }
        return 0;
    }
    
    /// 顶部状态栏高度（包括安全区）
    static func StatusBar_Height() -> CGFloat {
        var statusBarHeight: CGFloat = 0
        if #available(iOS 13.0, *) {
            let scene = UIApplication.shared.connectedScenes.first
            guard let windowScene = scene as? UIWindowScene else { return 0 }
            guard let statusBarManager = windowScene.statusBarManager else { return 0 }
            statusBarHeight = statusBarManager.statusBarFrame.height
        } else {
            statusBarHeight = UIApplication.shared.statusBarFrame.height
        }
        return statusBarHeight
    }
    
    /// 导航栏高度
    static func Navigation_BarHeight() -> CGFloat {
        return 44.0
    }
    
    /// 状态栏+导航栏的高度
    static func Navigation_FullHeight() -> CGFloat {
        return UIDevice.StatusBar_Height() + UIDevice.Navigation_BarHeight()
    }
    
    /// 底部导航栏高度
    static func TabBar_Height() -> CGFloat {
        return 49.0
    }
    
    /// 底部导航栏高度（包括安全区）
    static func TabBar_FullHeight() -> CGFloat {
        return UIDevice.TabBar_Height() + UIDevice.Bottom_safeDistance()
    }
}


import Photos

extension UIImage {
    // 保存图片到沙盒内,并返回存储图片的后缀地址
    class func saveImageInCache(_ image: UIImage) -> String? {
        guard let imageData = image.jpegData(compressionQuality: 0.6) else {
            return nil
        }
        // 获取当前时间戳拼接到文件尾部,防止存储图片多时地址重复
        let currentDate = Date()
        let currentTime = Int64(currentDate.timeIntervalSince1970 * 1000)
        let timeString = "\(currentTime)"
        
        // 这个路径是存储到本地用的图片后缀地址
        let savePath = "Documents/image_\(timeString).png"
        // 这个路径是将图片存入沙盒时的路径
        let imageAllPath = NSHomeDirectory().appending("/\(savePath)")
        // 存储图片到沙盒,并返回结果
        do {
            try imageData.write(to: URL(fileURLWithPath: imageAllPath))
            return savePath
        } catch {
            print("Failed to save compressed image: \(error)")
            return nil
        }
    }
    
    // 根据图片的后缀地址, 重新拼接完成路径获取图片
    class func loadLocalImage(_ imagePath: String) -> UIImage? {
        // 防止每次启动程序沙盒前缀地址改变,只存储后边文件路径,调用时再次拼接
        let imageAllPath = NSHomeDirectory().appending("/\(imagePath)")
        return UIImage(contentsOfFile: imageAllPath)
    }
    static func loadImage(_ imageName:String) -> UIImage {
        if imageName.contains("tab"){
            return UIImage(named: imageName)!
        }else{
            return UIImage.loadLocalImage(imageName)!
        }
    }
}
